package com.vil.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Active;
import com.vil.entity.Admin;
import com.vil.entity.Complaint;
import com.vil.entity.Mail;
import com.vil.entity.Repair;
import com.vil.service.ActiveService;
import com.vil.service.ComplaintService;
import com.vil.service.MailService;
import com.vil.service.RepairService;

@Controller
@RequestMapping("service")
public class ServerController extends BaseController {

	@Autowired
	private ActiveService activeService;

	@Autowired
	private MailService mailService;

	@Autowired
	private ComplaintService complaintService;

	@Autowired
	private RepairService repairService;

	/* 活动管理 */

	// 去活动页面
	@RequestMapping("activityManagement.html")
	public String activityManagement(Model model,HttpSession session) {
		Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
			return "admin/login";
		}
		ServerResponse serverResponse = activeService.getListByManyConditions(1, 5, null);
		model.addAttribute("active", serverResponse.getData());
		return "admin/activity_list";
	}

	// 获取活动列表
	@PostMapping("viewactivelist")
	@ResponseBody
	public ServerResponse vieweactivelist(
			@RequestParam(value = "condition", defaultValue = "", required = false) String condition,
			@RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize, Model model) {
		Active active = new Active();
		active.setOrganizer(condition);
		return activeService.getListByManyConditions(pageIndex, pageSize, active);
	}

	// 去新增页面
	@GetMapping("activity_add.html")
	public String active_add() {
		return "admin/activity_add";
	}

	// 新增活动项目
	@PostMapping("addActiveSelective")
	public ServerResponse addActiveSelective(Active active) {
		if (this.isActive() == false) {
			return ServerResponse.buildErrorMsg("您的账号未被激活，不能进行该操作");
		}
		return activeService.insertSelective(active);
	}

	// 去编辑页面
	@GetMapping("active_edit.html/{id}")
	public String active_edit(@PathVariable Long id, Model model) {
		ServerResponse byId = activeService.getById(id);
		model.addAttribute("e", byId.getData());
		return "admin/active_edit";
	}

	// 修改
	@PostMapping("editActive")
	@ResponseBody
	public ServerResponse editActiveSelective(Active active) {
		return activeService.updateByPrimaryKeySelective(active);
	}
	
	//去修改页面
	@DeleteMapping("deleteActiveById/{id}")
    @ResponseBody
    public ServerResponse deleteActiveById(@PathVariable Long id){

        return  activeService.deleteByPrimaryKey(id);
    }
	
	//修改
	@PostMapping("deleteActiveByIds")
    @ResponseBody
    public ServerResponse deleteActiveByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return activeService.deleteMany(array);
    }
	

	/* 邮箱管理 */
	// 去邮件页面
	@RequestMapping("mailboxAdministration.html")
	public String MailboxAdministration(Model model, HttpSession session) {
		Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
			return "admin/login";
		}
		ServerResponse serverResponse = mailService.getListByManyConditions(1, 5, null);
		model.addAttribute("mail", serverResponse.getData());
		return "admin/mailboxlist";
	}

	// 获取邮箱列表
	@PostMapping("viewmailboxlist")
	@ResponseBody
	public ServerResponse viewmailboxlist(
			@RequestParam(value = "condition", defaultValue = "", required = false) String condition,
			@RequestParam(value = "condition1",defaultValue = "",required = false) Date condition1,
            @RequestParam(value = "condition2",defaultValue = "",required = false) String condition2,
			@RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize, Model model) {
		Mail mail = new Mail();
		mail.setMail_title(condition);
		mail.setCreate_time(condition1);
		mail.setVillage(condition2);
		return mailService.getListByManyConditions(pageIndex, pageSize, mail);
	}

	// 去新增页面
	@GetMapping("mailboxadd.html")
	public String mail_add() {
		return "admin/mailboxadd";
	}

	// 新增邮箱项目
	@PostMapping("addMailSelective")
	public ServerResponse addMailSelective(Mail mail) {
		return mailService.insertSelective(mail);
	}

	// 去编辑页面
	@GetMapping("mail_edit.html/{id}")
	public String mail_edit(@PathVariable Long id, Model model) {
		ServerResponse byId = mailService.getById(id);
		model.addAttribute("e", byId.getData());
		return "admin/mail_edit";
	}

	// 修改
	@PostMapping("editMail")
	@ResponseBody
	public ServerResponse editMailSelective(Mail mail) {
		return mailService.updateByPrimaryKeySelective(mail);
	}
	
	//删除
	@DeleteMapping("deleteMailById/{id}")
    @ResponseBody
    public ServerResponse deleteMailById(@PathVariable Long id){

        return  mailService.deleteByPrimaryKey(id);
    }
    
	//删除
    @PostMapping("deleteMailByIds")
    @ResponseBody
    public ServerResponse deleteMailByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return mailService.deleteMany(array);
    }

	/* 投诉管理 */

	// 去投诉页面
	@RequestMapping("complaintManagement.html")
	public String complaintManagement(Model model, HttpSession session) {
		 Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
	        if (currentAdmin==null){
			return "admin/login";
		}
		ServerResponse serverResponse = complaintService.getListByManyConditions(1, 5, null);
		model.addAttribute("complaint", serverResponse.getData());
		return "admin/complainlist";
	}

	// 获取投诉列表
	@PostMapping("viewcomplainlist")
	@ResponseBody
	public ServerResponse viewcomplainlist(
			@RequestParam(value = "condition", defaultValue = "", required = false) Date condition,
			@RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
            @RequestParam(value = "condition2",defaultValue = "",required = false) String condition2,
			@RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize, Model model) {
		Complaint complaint = new Complaint();
		complaint.setCreate_time(condition);
		complaint.setComplaint_user(condition1);
		complaint.setComplaint_name(condition2);
		return complaintService.getListByManyConditions(pageIndex, pageSize, complaint);

	}

	// 去新增页面
	@GetMapping("complainadd.html")
	public String complain_add() {
		return "admin/complainadd";
	}

	// 新增投诉项目
	@PostMapping("addComplaintSelective")
	public ServerResponse addComplaintSelective(Complaint complaint) {
		return complaintService.insertSelective(complaint);
	}

	// 去编辑页面
	@GetMapping("complaint_edit.html/{id}")
	public String complaint_edit(@PathVariable Long id, Model model) {
		ServerResponse byId = complaintService.getById(id);
		model.addAttribute("e", byId.getData());
		return "admin/complaint_edit";
	}

	// 修改
	@PostMapping("editComplaint")
	@ResponseBody
	public ServerResponse editComplaintSelective(Complaint complaint) {
		return complaintService.updateByPrimaryKeySelective(complaint);
	}
	
	@DeleteMapping("deleteComplaintById/{id}")
    @ResponseBody
    public ServerResponse deleteComplaintById(@PathVariable Long id){

        return  complaintService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteComplaintByIds")
    @ResponseBody
    public ServerResponse complaintDetailByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return complaintService.deleteMany(array);
    }

	/* 保修管理 */

	// 去保修页面
	@RequestMapping("warrantyManagement.html")
	public String warrantyManagement(Model model, HttpSession session) {
		Admin currentAdmin = (Admin)session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
			return "admin/login";
		}
		ServerResponse serverResponse = repairService.getListByManyConditions(1, 5, null);
		model.addAttribute("repair", serverResponse.getData());
		return "admin/repairlist";
	}

	// 获取投诉列表
	@PostMapping("viewrepairlist")
	@ResponseBody
	public ServerResponse viewrepairlist(
			@RequestParam(value = "condition", defaultValue = "", required = false) String condition,
			@RequestParam(value = "condition1",defaultValue = "",required = false) String condition1,
            @RequestParam(value = "condition2",defaultValue = "",required = false) String condition2,
			@RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize, Model model) {
		Repair repair = new Repair();
		repair.setRepair_user(condition);
		repair.setEquipment_name(condition1);
		repair.setRepair_describe(condition2);
		return repairService.getListByManyConditions(pageIndex, pageSize, repair);

	}

	// 去新增页面
	@GetMapping("repairadd.html")
	public String repair_add() {
		return "admin/repairadd";
	}

	// 新增投诉项目
	@PostMapping("addRepairSelective")
	public ServerResponse addRepairSelective(Repair repair) {
		return repairService.insertSelective(repair);
	}

	// 去编辑页面
	@GetMapping("repair_edit.html/{id}")
	public String repair_edit(@PathVariable Long id, Model model) {
		ServerResponse byId = repairService.getById(id);
		model.addAttribute("e", byId.getData());
		return "admin/repair_edit";
	}

	// 修改
	@PostMapping("editRepair")
	@ResponseBody
	public ServerResponse editRepairSelective(Repair repair) {
		return repairService.updateByPrimaryKeySelective(repair);
	}
	
	@DeleteMapping("deleteRepairById/{id}")
    @ResponseBody
    public ServerResponse deleteRepairById(@PathVariable Long id){

        return  repairService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteRepairByIds")
    @ResponseBody
    public ServerResponse deleteRepairByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return repairService.deleteMany(array);
    }

}
